package com.atguigu.meinian.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.meinian.mapper.TravelItemMapper;
import com.atguigu.meinian.pojo.TravelItem;
import com.atguigu.meinian.service.TravelItemService;
import com.atguigu.meinian.vo.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zbystart
 * @create 2021-06-19 15:29
 */
@Service(interfaceClass = TravelItemService.class)
@Transactional
public class TravelItemServiceImpl implements TravelItemService {

    @Autowired
    private TravelItemMapper travelItemMapper;

    @Override
    public void add(TravelItem travelItem) {
        travelItemMapper.insert(travelItem);
    }

    @Override
    public PageResult queryTravelitemList(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<TravelItem> page = travelItemMapper.selectTravelitemList(queryString);
        return new PageResult(page.getTotal(),page.getResult(),page.getPageNum());
    }

    @Override
    public TravelItem show(Integer id) {
        return travelItemMapper.selectTravelitemById(id);
    }

    @Override
    public void update(TravelItem travelItem) {
        travelItemMapper.updateTravelItemById(travelItem);
    }

    @Override
    public void delete(Integer id) {
        // 查询关联关联
        int count = travelItemMapper.selectTravelItemRelation(id);
        if (count > 0) {
            throw new RuntimeException("删除自由行失败，请先删除关联信息");
        }
        travelItemMapper.deleteTravelItemById(id);
    }

}
